<template>
  <van-row>
    <van-nav-bar title="调用微信信息" left-text="返回" left-arrow @click-left="onClickLeft">
    </van-nav-bar>
    <van-row class="side-nav">
      <h1 class="vant-title">
        <img src="https://img.yzcdn.cn/vant/logo.png">
        <span>第三方登陆</span>
      </h1>
      <van-row>
        <h5>微信登陆</h5>
        <van-button type="default" class="getPhoto" @click="handleWechat">登陆</van-button>
        <van-button type="default" class="getPhoto" @click="shareWechat('text')">分享文字</van-button>
        <van-button type="default" class="getPhoto" @click="shareWechat('link')">分享链接</van-button>
        <van-button type="default" class="getPhoto" @click="shareWechat('image')">分享图片</van-button>
        <van-button type="default" class="getPhoto" @click="shareWechat('music')">分享音乐</van-button>
        <van-button type="default" class="getPhoto" @click="shareWechat('video')">分享视频</van-button>
        <van-button type="default" class="getPhoto" @click="shareWechat('mini')">分享小程序</van-button>
      </van-row>
    </van-row>
  </van-row>
</template>

<script>
import { Toast } from 'vant'
import { getToken, getInterface, getUserInfo, shareText, shareImage, shareLink, shareMusic, shareVideo, shareMini } from '../api/webchat'

export default {
  name: 'camera',
  data () {
    return {
      loading: false,
      finished: false,
      rubbishCategoryList: []
    }
  },
  methods: {
    handleWechat () {
      Toast('方法被调用了')
      // eslint-disable-next-line no-undef
      Wechat.isInstalled(function (installed) {
        // eslint-disable-next-line one-var
        let scope = 'snsapi_userinfo',
          state = '_' + (+new Date())
        // eslint-disable-next-line no-undef
        Wechat.auth(scope, state, function (response) {
          getToken(response).then(res1 => {
            getInterface(res1).then(res2 => {
              alert(JSON.stringify(res2))
              if (res2.data.errmsg === 'ok') {
                getUserInfo(res1).then(res => {
                  alert(JSON.stringify(res))
                })
              } else {
                alert('token失效，请重新获取token ')
              }
            })
          })
        }, function (reason) {
          alert('Failed: ' + reason)
        })
      }, function (reason) {
        alert('Failed: ' + reason)
      })
    },
    shareWechat (item) {
      if (item === 'text') {
        shareText('分享内容')
      } else if (item === 'link') {
        shareLink('分享内容')
      } else if (item === 'music') {
        shareMusic('分享内容')
      } else if (item === 'video') {
        shareVideo('分享内容')
      } else if (item === 'mini') {
        shareMini('分享内容')
      } else if (item === 'image') {
        shareImage('分享内容')
      }
    },
    onClickLeft () {
      this.$router.go(-1)
    }
  }
}
</script>

<style scoped>
.van-doc-demo-block__title{
  margin: 0;
  font-weight: 400;
  font-size: 14px;
  color: rgba(69,90,100,.6);
  text-align: left;
}
.side-nav{
  box-sizing: border-box;
  width: 100%;
  padding: 64px 10px 10px;
}
.side-nav .vant-desc{
  margin: 0 0 40px;
  color: #7d7e80;
  font-size: 14px;
  text-align: left;
}
.vant-title{
  font-weight: normal;
  -webkit-user-select: none;
  user-select: none;
  margin: 0 0 16px;
  text-align: left;
}
.side-nav .vant-title img {
  width: 36px;
}
.van-cell__title{
  text-align: left;
}
.comment{
  margin: 10px 0px;
}
.getPhoto{
  width: 100%;
}
#myImage{
  width: 100%;
}
</style>
